package com.dmx.adi;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;

public class AdiRemoteFileExtractor {
	private String remoteRootDir;

	private File adiXmlFile;

	final static private Logger log = LoggerFactory.getLogger(AdiRemoteFileExtractor.class);
	//public static DMXLogger log = DMXLogFactory.getLog(AdiRemoteFileExtractor.class);
	
	private static final String defaultQryString = "//*/Content";

	private String qryString = defaultQryString;

	public static final String adiFileDtd = "/ADI.DTD";

	private EntityResolver resolver = new EntityResolver() {
		public InputSource resolveEntity(String publicId, String systemId) {

			InputStream in = getClass().getResourceAsStream(adiFileDtd);
			return new InputSource(in);
		}
	};

	public List<URL> extractFiles() {
		SAXReader saxReader = new SAXReader();

		saxReader.setEntityResolver(resolver);

		Document docRead = null;
		List<Node> lstNode = null;
		List<URL> lstRet = new ArrayList<URL>();

		try {
			docRead = saxReader.read(adiXmlFile);
		} catch (DocumentException e) {
			//log.error("AMS Back-end Operator","AMS","Import Manager","Import",null,"failed to read adiXml:" + adiXmlFile.getAbsolutePath(),null,e);
			log.error("failed to read adiXml:" + adiXmlFile.getAbsolutePath(),e);
		}

		try {

			if (docRead != null) {
				lstNode = (List<Node>) docRead.selectNodes(defaultQryString);
			}

			if (lstNode != null) {
				for (Node node : lstNode) {

					String urlStr = this.remoteRootDir + node.valueOf("@Value");

					URL url = null;

					try {
						url = new URL(urlStr);
					} catch (MalformedURLException e) {
						log.error("Mal formed URL", e);

					}

					if (url != null) {
						lstRet.add(url);
					}
				}
			}

		} catch (Exception e) {
			//log.error("AMS Back-end Operator","AMS","Import Manager",null,"Import","Failed to extract files",null,e);
			log.error("Failed to extract files",e);
		}

		return lstRet;
	}

	public AdiRemoteFileExtractor(String remoteRootDir, File adiXmlFile)
			throws FileNotFoundException {

		if (adiXmlFile.exists() == false) {
			throw new FileNotFoundException(adiXmlFile.getAbsolutePath());
		}

		this.remoteRootDir = remoteRootDir;
		this.adiXmlFile = adiXmlFile;

		if (this.remoteRootDir.endsWith("/") == false) {
			this.remoteRootDir += "/";
		}

	}

	public String getRemoteRootDir() {
		return remoteRootDir;
	}

	public void setRemoteRootDir(String remoteRootDir) {
		this.remoteRootDir = remoteRootDir;
	}

	public File getAdiXmlFile() {
		return adiXmlFile;
	}

	public void setAdiXmlFile(File adiXmlFile) {
		this.adiXmlFile = adiXmlFile;
	}

	static public void main(String argv[]) {
		AdiRemoteFileExtractor instance = null;
		try {
			instance = new AdiRemoteFileExtractor("ftp://localhost/adf/d",
					new File("c:/adi.xml"));
		} catch (FileNotFoundException e) {
			//log.error("AMS Back-end Operator","AMS","Import Manager",null,"Import","Failed to find file",null,e);
			log.error("Failed to find file",e);
		}

		if (instance != null) {
			List<URL> lstUrl = instance.extractFiles();
			for (URL url : lstUrl) {
				System.out.println(url);
			}
		}

	}

	public String getQryString() {
		return qryString;
	}

	public void setQryString(String qryString) {
		this.qryString = qryString;
	}

}
